if (SERVER) then
	AddCSLuaFile("shared.lua");
	SWEP.Weight				= 5;
	SWEP.AutoSwitchTo		= false;
	SWEP.AutoSwitchFrom		= false;
end;

if (CLIENT) then
	SWEP.DrawAmmo			= true;
	SWEP.DrawCrosshair		= false;
	SWEP.ViewModelFOV		= 82;
	SWEP.ViewModelFlip		= true;
	SWEP.CSMuzzleFlashes	= true;
	
	-- This is the font that's used to draw the death icons.
	-- Not using lol <._.>.
	--surface.CreateFont("csd", ScreenScale(30), 500, true, true, "CSKillIcons");
	--surface.CreateFont("csd", ScreenScale(60), 500, true, true, "CSSelectIcons");
end;

SWEP.Category			= "LightRP 2";

SWEP.Spawnable			= false;
SWEP.AdminSpawnable		= false;

SWEP.Primary.Sound		= Sound("Weapon_AK47.Single");
SWEP.Primary.Recoil		= -1;
SWEP.Primary.Damage		= -1;
SWEP.Primary.NumShots	= -1;
SWEP.Primary.Cone		= -1;
SWEP.Primary.Delay		= -1;

SWEP.Primary.ClipSize		= -1;
SWEP.Primary.DefaultClip	= -1;
SWEP.Primary.Automatic		= false;
SWEP.Primary.Ammo			= "none";

SWEP.Secondary.ClipSize		= -1;
SWEP.Secondary.DefaultClip	= -1;
SWEP.Secondary.Automatic	= false;
SWEP.Secondary.Ammo			= "none";

SWEP.currentHoldType = "";

function SWEP:Initialize()
	self.HoldType = "pistol";
	self.holsterType = "normal";
end;

function SWEP:Deploy()
	self:SendWeaponAnim(ACT_VM_DRAW);
	
	if (SERVER) then
		self.Owner:SetNWBool("lgtrp_holstered", false);
	end;
	
	if (CLIENT) then
		self.PositionMode = 3;
		self.PositionTime = .3;
		self.PositionMul = 1;
	end;
	
	return true;
end;

function SWEP:Holster()
	self:SendWeaponAnim(ACT_VM_HOLSTER);
	
	return true;
end;

function SWEP:Think()
	local holstered = self.Owner:GetNWBool("lgtrp_holstered");

	if (!holstered and self.currentHoldType != self.holsterType) then
		self:SetWeaponHoldType(self.holsterType);
		self.currentHoldType = self.holsterType;
	elseif (holstered and self.currentHoldType != self.HoldType) then
		self:SetWeaponHoldType(self.HoldType);
		self.currentHoldType = self.HoldType;
	end;
	
	if (self.CustomThink) then
		self:CustomThink();
	end;
end;

function SWEP:Reload()
	local reloading = self.Weapon:DefaultReload(ACT_VM_RELOAD);
	
	if (reloading) then
		self.Weapon:EmitSound(self.ReloadSound);
	end;
end;

function SWEP:PrimaryAttack()
	self.Weapon:SetNextSecondaryFire(CurTime() +self.Primary.Delay);
	self.Weapon:SetNextPrimaryFire(CurTime() +self.Primary.Delay);
	
	if (!self.Owner:GetNWBool("lgtrp_holstered") or !self:CanPrimaryAttack()) then return; end;
	
	if (self.resetReloading) then
		self.Reloading = false;
	end;
	
	if (SERVER) then
		-- Play shoot sound.
		local primarySound = self.Primary.Sound;
		local soundType = type(primarySound);
		
		if (soundType == "table") then
			self.Owner:EmitSound(primarySound[math.random(1, #primarySound)]);
		else
			self.Owner:EmitSound(primarySound);
		end;
	end;
	
	-- Shoot the bullet.
	self:CSShootBullet(self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone);
	
	-- Remove 1 bullet from our clip.
	self:TakePrimaryAmmo(1);
	
	if (self.Owner:IsNPC()) then return; end;
	
	-- Punch the player's view.
	self.Owner:ViewPunch(Angle(math.Rand(-2, 2) *self.Primary.Recoil, math.Rand(-1.5, 1.5) *self.Primary.Recoil, 0));
end;

SWEP.NextSecondaryAttack = 0;

function SWEP:SecondaryAttack()
	if (!self.Owner:GetNWBool("lgtrp_holstered")) then return; end;
	if (!self.IronSightsPos) then return; end;
	if (self.NextSecondaryAttack > CurTime()) then return; end;
	
	self.NextSecondaryAttack = CurTime() +0.3;
end;

function SWEP:CSShootBullet(dmg, recoil, numbul, cone)
	numbul 	= numbul or 1;
	cone 	= cone 	 or 0.01;

	local bullet = {}
	bullet.Num 		= numbul
	bullet.Src 		= self.Owner:GetShootPos();		-- Source.
	bullet.Dir 		= self.Owner:GetAimVector();	-- Dir of bullet.
	bullet.Spread 	= Vector(cone, cone, 0);		-- Aim Cone.
	bullet.Tracer	= 4;							-- Show a tracer on every x bullets.
	bullet.Force	= 13;							-- Amount of force to give to phys objects.
	bullet.Damage	= dmg;
	
	self.Owner:FireBullets(bullet);
	self.Weapon:SendWeaponAnim(ACT_VM_PRIMARYATTACK); 	-- View model animation.
	self.Owner:MuzzleFlash();							-- Crappy muzzle light.
	self.Owner:SetAnimation(PLAYER_ATTACK1);			-- 3rd Person Animation.
	
	if (self.Owner:IsNPC()) then return; end;
	
	if ((SinglePlayer() and SERVER) or (!SinglePlayer() and CLIENT)) then
		local eyeang = self.Owner:EyeAngles();
		eyeang.pitch = eyeang.pitch -recoil;
		
		self.Owner:SetEyeAngles(eyeang);
	end;
	
	return bullet;
end;

function SWEP:DrawWeaponSelection(x, y, wide, tall, alpha)
	-- Set us up the texture.
	surface.SetDrawColor(255, 255, 255, alpha);
	surface.SetTexture(self.WepSelectIcon);
	
	-- Lets get a sin wave to make it bounce.
	local fsin = 0;
	
	if (self.BounceWeaponIcon == true) then
		fsin = math.sin(CurTime() *10) *5;
	end;
	
	-- Borders.
	y = y +10;
	x = x +10;
	wide = wide -20;
	
	--Draw that mother.
	surface.DrawTexturedRect(x +(fsin), y -(fsin),  wide -fsin *2 , (wide /2) +(fsin));
	
	--Draw weapon info box.
	self:PrintWeaponInfo(x +wide +20, y +tall *0.95, alpha);
end;

SWEP.PositionMode = 3;
SWEP.PositionTime = .2;
SWEP.PositionMul = 0;

function SWEP:GoToPosition(pos, ang, newpos, newang)
	local mul = self.PositionMul;
	
	if (newang) then
		ang:RotateAroundAxis(ang:Right(),	mul *newang.x);
		ang:RotateAroundAxis(ang:Up(),		mul *newang.y);
		ang:RotateAroundAxis(ang:Forward(), mul *newang.z);
	end;
	
	pos = pos +newpos.x *ang:Right() 	* mul;
	pos = pos +newpos.y *ang:Forward()  * mul;
	pos = pos +newpos.z *ang:Up() 	 	* mul;
	
	return pos, ang;
end;

function SWEP:GetViewModelPosition(pos, ang)
	-- Going to ironsights.
	if (self.PositionMode == 1) then
		if (self.IronSightPos) then
			pos, ang = self:GoToPosition(pos *1, ang *1, self.IronSightPos, self.IronSightAng or Vector(0, 0, 0));
		end;
		
	-- Holstering/unholstering.
	elseif (self.PositionMode == 2 or self.PositionMode == 3) then
		if (self.HolsteredPos) then
			pos, ang = self:GoToPosition(pos *1, ang *1, self.HolsteredPos, self.HolsteredAng or Vector(0, 0, 0));
		end;
	end;
	
	return pos, ang;
end;